﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SGRapidForm.Common;
using SGRapidForm.Permission.Service.Interface;
using SGRapidForm.Permission.Entity;
using DevExpress.Web.ASPxCallback;
using System.Collections.Specialized;

namespace SGDemo.Web
{
    public partial class ChooseToDept : PageBase
    {
        private IPermissionService _permissionService = ServiceProxyFactory.Create<IPermissionService>("permissionservice");

        protected void Page_Load(object sender, EventArgs e)
        {
           if(!this.IsPostBack)
           {
               Bind();
           }
        }

        /// <summary>
        /// 部门绑定
        /// </summary>
        private void Bind()
        {
            List<Dept> deptList = _permissionService.Dept_GetByUserKeyid(ApplicationContext.Current.UserKeyid);
            //只有一个可登录部门选择时候，直接忽略此页，跳转到其他页
            if (deptList.Count == 1)
            {
                ApplicationContext.Current.CurrentDeptKeyid = deptList[0].Keyid;
                ApplicationContext.Current.CurrentDeptCode = deptList[0].DeptCode;
                ApplicationContext.Current.CurrentDeptName = deptList[0].DeptName;
                ApplicationContext.Current.CurrentDeptType = deptList[0].DeptType;
                this.Response.Redirect("Main.aspx", true);
            }

            rptDeptList.DataSource = deptList;
            rptDeptList.DataBind();
        }

        /// <summary>
        /// 选择部门
        /// </summary>
        protected void cabExecute_Callback(object sender, CallbackEventArgs e)
        {
            NameValueCollection parameters = HttpUtility.ParseQueryString(e.Parameter);
            ApplicationContext.Current.CurrentDeptKeyid = parameters["DeptKeyid"];
            ApplicationContext.Current.CurrentDeptCode = parameters["DeptCode"];
            ApplicationContext.Current.CurrentDeptName = parameters["DeptName"];
            ApplicationContext.Current.CurrentDeptType = parameters["DeptType"];
            //操作成功
            e.Result = "Success";
        }
    }
}